package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.EmployeeDTO;
import com.wjj.sixinone.common.pojo.dto.base.BasePage;
import com.wjj.sixinone.common.pojo.entity.Department;
import com.wjj.sixinone.common.pojo.entity.Employee;
import com.wjj.sixinone.common.pojo.excel.EmployeeExcel;
import com.wjj.sixinone.common.pojo.vo.EmployeeForm;
import com.wjj.sixinone.common.pojo.vo.EmployeeVo;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.EmployeeMapper;
import com.wjj.sixinone.service.DepartmentService;
import com.wjj.sixinone.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjj.sixinone.service.minio.MinioFileService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MinioFileService fileService;
    @Autowired
    private ExcelExportHandle exportHandle;
    @Override
    public Result listDepartmentName() {
        List<Department> list = departmentService.list(new LambdaQueryWrapper<Department>().select(Department::getDepartmentName));



        return Result.ok().put("data", list);
    }

    @Override
    public Result selectEmployeeByLimit(EmployeeVo employeeVo) {
        Integer page = employeeVo.getPage();
        Integer limit = employeeVo.getLimit();
        int index = (page - 1) * limit;
        EmployeeDTO employeeDTO = new EmployeeDTO();
        BeanUtils.copyProperties(employeeVo, employeeDTO);
        employeeDTO.setIndex(index);
        List<Employee> employees=employeeMapper.selectEmployeeByLimit(employeeDTO);
        Integer count =employeeMapper.count(employeeDTO);

        return Result.ok().put("data", employees).put("count", count);
    }

    @Override
    public Result updateEmployeeById(EmployeeForm employeeForm) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeForm, employee);
        int i = employeeMapper.updateById(employee);
        if (i <1){
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);
        }
        return Result.ok();
    }

    @Override
    public Result addEmployee(EmployeeForm employeeForm) {
        Employee employee = new Employee();
        String img = employeeForm.getImg();
        String url = fileService.uploadBase64File(img);
        BeanUtils.copyProperties(employeeForm, employee);
        employee.setImg(url);
        int i = employeeMapper.insert(employee);
        if (i <1){
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }

        return Result.ok();
    }

    @Override
    public void uploadExcel(HttpServletResponse httpServletResponse) {
        List<Employee> employees = employeeMapper.selectList(new LambdaQueryWrapper<Employee>().select(Employee::getId, Employee::getEmployeeName,
                        Employee::getEmployeeAge, Employee::getEmployeeGender,
                        Employee::getEmployeePosition, Employee::getEmployeeSalary,
                        Employee::getEmployeeTel, Employee::getDepartmentName, Employee::getImg,
                        Employee::getCreateUser, Employee::getUpdateUser, Employee::getCreateTime, Employee::getUpdateTime,
                        Employee::getEmployeeWorkDate
                )
        );
        List<EmployeeExcel> employeeExcels=new ArrayList<>();
        for (Employee employee : employees) {
            EmployeeExcel employeeExcel = new EmployeeExcel();
            BeanUtils.copyProperties(employee, employeeExcel);
            employeeExcels.add(employeeExcel);
        }
        exportHandle.export( httpServletResponse, "员工信息", employeeExcels,EmployeeExcel.class);

    }

    @Override
    public Result deleteById(Map<String, String> map) {
        String id = map.get("id");
        String img = map.get("img");
        if(img!=null) {
            fileService.deleteFileFromMinio(img);
        }

        int i = employeeMapper.deleteById(id);
        if (i <1){
            throw new GlobalException(ExceptionEnum.DELETE_ERROR);
        }

        return Result.ok();
    }

    @Override
    public Result getEmployeeNameByDepartmentName(String departmentName) {
        List<Employee> employeeNameByDepartmentName = employeeMapper.getEmployeeNameByDepartmentName(departmentName);
        return Result.ok().put("data",employeeNameByDepartmentName);
    }

    @Override
    public Result selectEmployeeByDepartmentName() {
        List<Employee> employees = employeeMapper.selectList(new LambdaQueryWrapper<Employee>()
                .select(Employee::getEmployeeName)
        );

        Set<String> employeeNames = employees.stream()
                .map(Employee::getEmployeeName)
                .collect(Collectors.toSet());

        return Result.ok().put("data",employeeNames);
    }
}
